package com.spsoft.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.spsoft.core.constants.CacheConstant;
import com.spsoft.system.entity.BiDataConfigInfo;
import com.spsoft.system.mapper.BiDataConfigInfoMapper;
import com.spsoft.system.service.BiDataConfigInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spsoft.vms.redis.RedisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *      应用参数配置 服务实现类
 * </p>
 *
 * @author dlchen
 * @since 2023-11-07
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class BiDataConfigInfoServiceImpl extends ServiceImpl<BiDataConfigInfoMapper, BiDataConfigInfo> implements BiDataConfigInfoService {
    private final RedisService redisService;

    /**
     * 启动加载应用参数到缓存
     */
    @PostConstruct
    public void init()
    {
        log.info("************init  初始化【应用参数】到缓存开始*************");
        this.upadteBiDataConfigInfoToCache();
        log.info("************init  初始化【应用参数】到缓存结束*************");
    }
    @Override
    public boolean upadteBiDataConfigInfoToCache() {

       try{
           this.baseMapper.selectList(Wrappers.<BiDataConfigInfo>query().lambda());
           List<BiDataConfigInfo> list = this.list();
           Map<String, List<BiDataConfigInfo>> listMap = list.stream().collect(Collectors.groupingBy(BiDataConfigInfo::getProjectCode));
           redisService.setCacheMap(CacheConstant.SYS_CACHE_APP_PARAM,listMap);
           log.info("************  初始化【应用参数】到缓存成功,缓存数据：【{}】*************",listMap.size());
           return true;
       } catch (Exception e) {
           log.error("************  初始化【应用参数】到缓存异常{}*************", e.getMessage());
           return false;
       }
    }

    /**
     * @return
     */
    @Override
    public String getEDonwloadUrlPrefix() {
//        LambdaQueryWrapper<BiDataConfigInfo> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(BiDataConfigInfo::getProjectCode, "vatmgr")
//                .eq(BiDataConfigInfo::getParamsCode, "EINVOICE_HOST_PORT_BW_XZ_GW");
//        BiDataConfigInfo info = baseMapper.selectOne(wrapper);
//
//        return info == null ? null : info.getParamsValues();

        Map<String, List<BiDataConfigInfo>> config = redisService.getCacheMap(CacheConstant.SYS_CACHE_APP_PARAM);
        List<BiDataConfigInfo> vatmgr = config.get("vatmgr");
        if (CollectionUtils.isNotEmpty(vatmgr)) {
            Map<String, BiDataConfigInfo> collect = vatmgr.stream().collect(Collectors.toMap(BiDataConfigInfo::getParamsCode, v -> v));
            BiDataConfigInfo info = collect.get("EINVOICE_HOST_PORT_BW_XZ_GW");
            return info == null ? null : info.getParamsValues();
        }
        return null;
    }

    /**
     * @return
     */
    @Override
    public String getESendUrlPrefix() {
//        LambdaQueryWrapper<BiDataConfigInfo> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(BiDataConfigInfo::getProjectCode, "vatmgr")
//                .eq(BiDataConfigInfo::getParamsCode, "EINVOICE_HOST_PORT_BW_KP");
//        BiDataConfigInfo info = baseMapper.selectOne(wrapper);
//
//        return info == null ? null : info.getParamsValues();
        Map<String, List<BiDataConfigInfo>> config = redisService.getCacheMap(CacheConstant.SYS_CACHE_APP_PARAM);
        List<BiDataConfigInfo> vatmgr = config.get("vatmgr");
        if (CollectionUtils.isNotEmpty(vatmgr)) {
            Map<String, BiDataConfigInfo> collect = vatmgr.stream().collect(Collectors.toMap(BiDataConfigInfo::getParamsCode, v -> v));
            BiDataConfigInfo info = collect.get("EINVOICE_HOST_PORT_BW_KP");
            return info == null ? null : info.getParamsValues();
        }
        return null;
    }

}
